package org.example.strategy.controller;

import org.example.strategy.service.PayStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 工厂 + 策略模式
     * @param strategyName : 策略名称
     * @param param : 方法参数
     * @return
     */
    @GetMapping
    public String strategy(@RequestParam String strategyName,
                           @RequestParam String param) {
        // Spring工厂中获取策略对象
        PayStrategy payStrategy = applicationContext.getBean(strategyName, PayStrategy.class);
        // 执行策略
        return payStrategy.pay(param);
    }

}
